package com.yiba.vpn.controller;

import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletRequest;

import com.yiba.vpn.util.HttpUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.yiba.vpn.authorization.manager.TokenManager;
import com.yiba.vpn.config.Constants;
import com.yiba.vpn.config.ResultStatus;
import com.yiba.vpn.dto.RouterDto;
import com.yiba.vpn.dto.SuperPojo;
import com.yiba.vpn.model.ResultModel;
import com.yiba.vpn.service.RouterSerivce;
import com.yiba.vpn.util.Iputil;
import com.yiba.vpn.util.RandomUtil;

/**
 * @author : tang
 * @createtime ：2018年2月28日 上午10:57:52
 *
 */
@Api(value = "wfee路由器")
@RestController
@RequestMapping("/router")
public class RouterController {

	private static Logger logger = Logger.getLogger(RouterController.class);

	@Autowired
	private RouterSerivce routerSerivce;

	@Autowired
	private TokenManager tokenManager;

	@RequestMapping(value = "/sum", method = RequestMethod.POST)
	@ApiOperation(value = "获取路由器预约数")
	public ResponseEntity<ResultModel> sum() {
		return routerSerivce.getSubscribeNum();
	}

	@RequestMapping(value = "/getcode", method = RequestMethod.POST)
	@ApiOperation(value = "获取短信验证码")
	public ResponseEntity<ResultModel> getCode(HttpServletRequest request, @RequestBody RouterDto routerDto) {

		String xfor = HttpUtils.getIpAddr(request);
		String ipforward = xfor == null ? null : xfor.split(",")[0];
		String ip = ipforward == null ? Iputil.getRequestIp(request) : ipforward;
		if ("172.31.26.124".equals(ip) || "172.31.29.195".equals(ip) || "0:0:0:0:0:0:0:1".equals(ip)
				|| "127.0.0.1".equals(ip)) {
			ip = null;
		}
		if (!com.yiba.vpn.util.StringUtils.isNullOrEmpty(ip)) {

			if (tokenManager.getValue("BLACK_IP_NOTE" + ip) != null) {
				logger.error(ipforward + "; 恶意攻击 ！该IP一分钟内访问发送短信服务多次  禁止访问 ");
				tokenManager.incr("NOTE_BLACK_" + ip, Constants.IP_EXPIRES_SECONDS);
				return new ResponseEntity<>(ResultModel.error(ResultStatus.REQUEST_AGAIN_ERROR), HttpStatus.OK);
			}
			long times = tokenManager.incr("NOTE_BLACK_" + ip, Constants.IP_EXPIRES_SECONDS);
			logger.info(ipforward + "; 该IP一分钟内访问发送短信服务  >>" + times + " 次 ; 号码是 " + routerDto.getTelephoneNum());
			if (times > 2) {
				logger.error(ipforward + "; 恶意攻击 ！该IP一分钟内访问发送短信服务  >>" + times + " 次");
				// 记下该请求的IP BLACK_IP_NOTE
				tokenManager.addV("BLACK_IP_NOTE" + ip, ip, Constants.BLACK_IP_DISABLE_VIST_RECORD, TimeUnit.SECONDS);
				return new ResponseEntity<>(ResultModel.error(ResultStatus.REQUEST_AGAIN_ERROR), HttpStatus.OK);
			}
		} else {
			logger.error(ipforward + "Ip 不正常   >>; 号码是 " + routerDto.getTelephoneNum());
		}

		return routerSerivce.getCode(routerDto);
	}

	@RequestMapping(value = "/subscribe", method = RequestMethod.POST)
	@ApiOperation(value = "预约")
	public ResponseEntity<ResultModel> subscribe(@RequestBody RouterDto routerDto) {
		return routerSerivce.subscribe(routerDto);
	}

	@RequestMapping(value = "/getuserinfo", method = RequestMethod.POST)
	@ApiOperation(value = "获取当前用户信息")
	public ResponseEntity<ResultModel> getUserInfo(@RequestBody RouterDto routerDto) {
		return routerSerivce.getUserInfo(routerDto);
	}

	@RequestMapping(value = "/savewallet", method = RequestMethod.POST)
	@ApiOperation(value = "手机用户填写以太网地址")
	public ResponseEntity<ResultModel> savewallet(@RequestBody RouterDto routerDto) {
		return routerSerivce.saveWallet(routerDto);
	}

	@RequestMapping(value = "/updaterandom", method = RequestMethod.GET)
	@ApiOperation(value = "动态更改数据跳动范围")
	public ResponseEntity<ResultModel> updateRandom(Integer begain, Integer count) {
		if (begain == null || count == null) {
			return new ResponseEntity<ResultModel>(ResultModel.error(ResultStatus.REQUEST_PARAMS_ERROR), HttpStatus.OK);
		}
		RandomUtil.routerStartNum = begain;
		RandomUtil.routerCountNum = count;
		SuperPojo pojo = new SuperPojo();
		pojo.put("routerStartNum", RandomUtil.routerStartNum);
		pojo.put("routerCountNum", RandomUtil.routerCountNum);
		return new ResponseEntity<ResultModel>(ResultModel.ok(pojo), HttpStatus.OK);
	}

	@RequestMapping(value = "/robot", method = RequestMethod.GET)
	@ApiOperation(value = "自动插入数据")
	public ResponseEntity<ResultModel> robot(Integer count) {
		return routerSerivce.robot(count);
	}



}
